home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / nn.zip / OPTIONS.H < prev    next >
C/C++ Source or Header  |  1989-06-28  |  2KB  |  89 lines

  1. /*
  2.  * options.h - include file for generic option parsing 
  3.  *
  4.  * (c) Copyright 1988, Kim F. Storm, storm@texas.dk
  5.  */
  6.  
  7. /*
  8.  * To use this routine, you must a table called an Option_Description.
  9.  * Each element in this table describes one possible option: 
  10.  *    Its option letter
  11.  *    Its argument type (if any)
  12.  *    Whether an argument is mandatory or optional 
  13.  *    The address of the variable holding the option value
  14.  *    The defualt value if argument is optional
  15.  *
  16.  * Example:
  17.  *
  18.  *    A program accepts the following options:
  19.  *        -a    [no value]
  20.  *        -b N    [a numeric value]
  21.  *        -p [N]    [an optional numeric value]
  22.  *        -t S    [a string value]
  23.  *
  24.  * The corresponding option description table would then look like:
  25.  * 
  26.  *    #include <options.h>
  27.  *    int a_flg = 1, b_value = 0, p_value = 0;
  28.  *    char *t_string = "default";
  29.  *
  30.  *    Option_Description( options ) {
  31.  *        'a', Bool_Option(a_flg),
  32.  *        'b', Int_Option(b_value),
  33.  *        'p', Int_Option_Optional(p_value, -1),
  34.  *        't', String_Option(t_string),
  35.  *        '\0',
  36.  *     }
  37.  * To parse the argument list - and the contents of the environment variable 
  38.  * XXINIT, all that has to be done is to issue the following call:
  39.  *
  40.  *    files = parse_options(argc, argv, "XXINIT", options, NULL);
  41.  *
  42.  * If no environment variable is associated with the program, use NULL as
  43.  * the third parameter.
  44.  *
  45.  * Upon return, the elements argv[1] .. argv[files] will contain
  46.  * the file names (and other 'non-options') that occur in the argument list.
  47.  *
  48.  * The last NULL argument may be replaced by your own 'usage routine'
  49.  * which will be called in the following way:
  50.  *
  51.  *    usage(pname)
  52.  *    char *pname; /+ argv[0] without path +/
  53.  *
  54.  * 
  55.  * char *program_name(argv)
  56.  *
  57.  * return a pointer to the last component of argv[0] (the program name with
  58.  * with the path deleted).
  59.  *
  60.  
  61.  */
  62.  
  63.  
  64. struct option_descr {
  65.     char    option_letter;
  66.     char    option_type;
  67.     char **    option_address;
  68.     char *    option_default;
  69. } ;
  70.  
  71.  
  72. #define    Option_Description(name) \
  73.     struct option_descr name[] =
  74.  
  75. #define    Bool_Option(addr) \
  76.     1, (char **)(&addr), (char *)0
  77.  
  78. #define    String_Option(addr) \
  79.     2, &addr, (char *)0
  80.  
  81. #define String_Option_Optional(addr, default) \
  82.     3, &addr, default
  83.  
  84. #define    Int_Option(addr) \
  85.     4, (char **)(&addr), (char *)0
  86.  
  87. #define    Int_Option_Optional(addr, default) \
  88.     5, (char **)(&addr), (char *)default
  89.